package com.chijun.mform.field.impl;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.chijun.mform.R;
import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.create.mode.FormFieldOption;
import com.chijun.mform.field.BaseField;
import com.example.ccj.u.ActivityUtils;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.ToastUtils;
import com.github.gzuliyujiang.wheelpicker.OptionPicker;

import java.util.List;

import io.reactivex.ObservableEmitter;

/**
 * 单项选择
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/12/23 15:43
 */
public class FieldFor03DanXiangXuanZe extends BaseField {
    public FieldFor03DanXiangXuanZe(){
        super();
    }


    /**
     * 填写
     * @param context 上下文
     * @param writeFormClass 1
     * @return 1
     */
    @Override
    public View getWritelView(Context context, WriteFormClass writeFormClass) {

        FormField formField = writeFormClass.formField;
        List<FormFieldOption> formFieldOptionList = writeFormClass.writeFormData.getFormFieldOptionMap().get(formField.getId());
        
        //提示文本
        if (formField.getHint() != null) writeFormClass.fieldFullContentView.setHint(formField.getHint());
        //输入文本颜色
        if (formField.getTextColor() != 0) writeFormClass.fieldFullContentView.setTextColor(formField.getTextColor());
        //默认文本
        String textFieldZanCunData = getTextFieldZanCunData(context,"fieldId=?",String.valueOf(formField.getId()));
        if (textFieldZanCunData == null) textFieldZanCunData = formField.getDefaultContent();
        if (textFieldZanCunData != null) {
            if (formFieldOptionList != null) {
                saveValue = textFieldZanCunData;
                for (FormFieldOption formFieldOption : formFieldOptionList) {
                    if (String.valueOf(formFieldOption.id).equals(textFieldZanCunData)) {
                        writeFormClass.fieldFullContentView.setText(formFieldOption.content);
                    }
                }
                writeFormClass.inputIsOK = true;
            } else {
                LogUtils.e("存在选项但未获取到选项配置！");
            }
        }

        writeFormClass.fieldFullContentView.setKeyListener(null);
        writeFormClass.fieldFullContentView.setFocusable(false);
        writeFormClass.fieldFullContentView.setCheckInterval(2000);
        writeFormClass.fieldFullContentView.setOnClickListener(v -> {
            if (formFieldOptionList == null || formFieldOptionList.isEmpty()) {
                ToastUtils.showShort("该字段未配置选项！");
                return;
            }

            OptionPicker optionPicker = new OptionPicker(ActivityUtils.getTopActivity());
            optionPicker.setData(formFieldOptionList);
            optionPicker.setTitle(formField.getName());
            optionPicker.getWheelView().setFormatter(item -> {
                if (item instanceof FormFieldOption) return ((FormFieldOption)item).content;
                return item.toString();
            });

            optionPicker.setOnOptionPickedListener((position, item) -> {
                if (item instanceof FormFieldOption) {
                    FormFieldOption f = (FormFieldOption)item;
                    writeFormClass.fieldFullContentView.setText(f.content);
                    saveValue = String.valueOf(f.id);
                    writeFormClass.inputIsOK = true;
                    if (writeFormClass.optionChangeListener != null) {
                        writeFormClass.optionChangeListener.onChanged(formField.getId(),f);
                    }
                }
            });
            optionPicker.show();
        });

        return writeFormClass.view;
    }


    /**
     * 展示
     * @param context  上下文
     * @param fullModel 1
     * @return 1
     */
    @Override
    protected View getShowFullView(Context context, FullClass fullModel) {
        //字段名属性设置
        fullModel.setFieldProperty();

        String fieldValue = fullModel.getFieldValue();
        if (fieldValue != null) {
            fullModel.loadContent.setText(fieldValue);
        }
        fullModel.loadContent.setKeyListener(null);
        fullModel.loadContent.setFocusable(false);
        fullModel.loadContent.setTextIsSelectable(true);//可选择

        //有修改权限时可修改
        if (fullModel.isHaveModifyPower()) {
            List<FormFieldOption> formFieldOptions = fullModel.getServerFullData().formFieldOptionMap.get(fullModel.formField.getId());
            fullModel.setJumpImageProperty(R.drawable.field_icon_riqixuanze, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    OptionPicker optionPicker = new OptionPicker(ActivityUtils.getTopActivity());
                    optionPicker.setData(formFieldOptions);
                    optionPicker.setTitle(fullModel.formField.getName());
                    optionPicker.getWheelView().setFormatter(item -> {
                        if (item instanceof FormFieldOption) return ((FormFieldOption)item).content;
                        return item.toString();
                    });

                    optionPicker.setOnOptionPickedListener((position, item) -> {
                        if (item instanceof FormFieldOption) {
                            FormFieldOption f = (FormFieldOption)item;
                            fullModel.loadContent.setText(f.content);
                            //写入临时数据内，提交时一起提交
                            processModifyFormFieldDataMap.put(fullModel.formField.getId(),String.valueOf(f.id));
                            //回调该选项，检查是否存在需要隐藏或显示的字段
                            if (fullModel.optionChangeListener != null) fullModel.optionChangeListener.onChanged(fullModel.formField.getId(),f);
                        }
                    });
                    optionPicker.show();
                }
            });
        }

        return fullModel.layout;
    }

    /**
     * 设置
     * @param context
     * @param installClass
     * @return
     */
    @Override
    protected View getFieldInstallView(Context context, InstallClass installClass) {
        ObservableEmitter<ViewGroup> emitter = installClass.emitter;

        emitter.onNext(installClass.getOptionView());        //选项设置
        emitter.onNext(installClass.getIsMastView());        //是否必填
        emitter.onNext(installClass.getIsCanScanView());     //是否可扫码
        emitter.onNext(installClass.getIsPrintView());        //是否参与打印

        return installClass.v;
    }


}
